package com.cq.controller;

import com.cq.pojo.Dorm;
import com.cq.service.DormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping("/student/normal")
public class DormManageController {

    @Autowired
    private DormService dormService;

    @RequestMapping("/queryDorms")
    public String queryDorms(ModelMap modelMap){
        List<Dorm> dorms = dormService.queryAllDorms();
        modelMap.put("dorms",dorms);
        return "dormsManage";
    }

    @RequestMapping("/add")
    public String addDorm(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String dor_id = req.getParameter("dor_id");
        String dor_sex = req.getParameter("dor_sex");
        String dor_num = req.getParameter("dor_num");
        String num = dor_num.substring(0,1);
        String dor_fact = req.getParameter("dor_fact");
        String dor_price = req.getParameter("dor_price");
        String dor_tel = req.getParameter("dor_tel");
        String build_id = req.getParameter("build_id");
        Dorm dorm = new Dorm(Integer.parseInt(dor_id),dor_sex,Integer.parseInt(num),
                Integer.parseInt(dor_fact),Double.parseDouble(dor_price),dor_tel,Integer.parseInt(build_id));
        int result = dormService.addDorm(dorm);
        PrintWriter out = resp.getWriter();
        if (result > 0){
            Integer integer = dormService.updateBuildDorm(Integer.parseInt(build_id));
            if(integer>0){
                out.flush();
                out.write("ok");
                out.close();
            }
        }else {
            out.flush();
            out.print("fail");
            out.close();
        }
        return "dormsManage";
    }

    @RequestMapping("/edit")
    public String editDorm(HttpServletResponse resp,String id,String sex,String num,String fact,String price,String tel,String build_id) throws IOException{
        System.out.println(id+","+sex+","+num+","+fact+","+price+","+tel+","+build_id);
        int result = dormService.updateDormById(new Dorm(Integer.parseInt(id),sex,Integer.parseInt(num),
                Integer.parseInt(fact),Double.parseDouble(price),tel,Integer.parseInt(build_id)));
        PrintWriter out = resp.getWriter();
        if (result > 0){
            out.flush();
            out.write("ok");
            out.close();
        }else {
            out.flush();
            out.print("fail");
            out.close();
        }
        return "dormsManage";
    }

    @RequestMapping("/del")
    public String delDorm(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String build_id = req.getParameter("build_id");
        System.out.println("=====================>"+build_id);
        int result = dormService.deleteDormById(Integer.parseInt(id));
        PrintWriter out = resp.getWriter();
        if (result > 0){
            Integer integer = dormService.deleteBuildDorm(Integer.parseInt(build_id));
            if(integer>0){
                out.flush();
                out.write("ok");
                out.close();
            }
        }else {
            out.flush();
            out.print("fail");
            out.close();
        }
        return "dormsManage";
    }
}
